<?php

namespace catchAdmin\business\controller;

use catcher\base\CatchRequest as Request;
use catcher\CatchResponse;
use catcher\base\CatchController;
use catchAdmin\business\model\Trade as TradeModel;
use think\Response;
use catcher\CatchAuth;
use think\facade\Db;
class Trade extends CatchController
{
    
    protected $tradeModel;
    
    /**
     *
     * @time 2022/02/25 17:32
     * @param TradeModel $tradeModel
     * @return mixed
     */
    public function __construct(TradeModel $tradeModel)
    {
        $this->tradeModel = $tradeModel;
    }

     /**
     *
     * @time 2022/02/15 11:41
     * @throws \think\db\exception\DbException
     * @return \think\response\Json
     */
    public function index(CatchAuth $auth)
    {
        $user = $auth->user();
        return CatchResponse::paginate($this->tradeModel->getList($user['tenant_id']));
    }

    /**
     *
     * @time 2022/02/15 11:41
     * @throws \think\db\exception\DbException
     * @return \think\response\Json
     */
    public function create(CatchAuth $auth,Request $request)
    {
        $params = $request->filterEmptyField()->post();
        Db::startTrans();
        $sqls=[];
        try {
            $user = $auth->user();
            // 基本信息
            if(!empty($params['basic'])) {
                $params['basic']['job_number'] = 'NM'.date('YmdHis');
                $params['basic']['tenant_id'] = $user['tenant_id'];
                $params['basic']['business_type'] = 'NM';
                $params['basic']['creator_id'] = $user['id'];
                $this->tradeModel->storeBy($params['basic']);
            } else {
                return CatchResponse::fail('新建失败');
            }

            //其他信息
            if(!empty($params['otherinfo'])) {
                $data['datas'] = json_encode($params['otherinfo']);
                $data['job_number'] = $params['basic']['job_number'];
                $data['tenant_id'] = $user['tenant_id'];
                $data['created_at'] = time();
                $data['updated_at'] = time();
                Db::table('db_otherinfo')->insert($data);
            }

            //报关单证
            if(!empty($params['declaration'])) {
                $params['declaration']['job_number'] =  $params['basic']['job_number'];
                $params['declaration']['tenant_id'] = $user['tenant_id'];
                $params['declaration']['created_at'] = time();
                $params['declaration']['updated_at'] = time();
                DB::name('db_declare_document')->insert($params['declaration']);
            }
            //运输明细
            if(!empty($params['transport'])) {
                foreach ($params['transport'] as $key => $value){
                    unset($value['key']);
                    $value['job_number'] =  $params['basic']['job_number'];
                    $value['tenant_id'] = $user['tenant_id'];
                    $value['created_at'] = time();
                    $value['updated_at'] = time();
                    DB::name('db_transport')->insert($value);
                }
            }
            Db::commit();
            return CatchResponse::success($sqls, '新建成功');
        } catch (\Exception $exception) {
            Db::rollback();
            return CatchResponse::fail('新建失败');
        }
//         $params = $request->filterEmptyField()->post();
//         Db::startTrans();
//         try {
//             $user = $auth->user();
//             $params['tenant_id'] = $user['tenant_id'];
//             $params['job_number'] = 'AE'.date('YmdHis');
//
//             if(!empty($params['declaration'])) {
//                 //报关表
//                 foreach ($params['declaration'] as $key => $value){
//                      unset($value['key']);
//                      $value['job_number'] =  $params['job_number'];
//                      $value['created_at'] = time();
//                      $value['updated_at'] = time();
//                      DB::name('db_declare_document')->insert($value);
//                 }
//             }
//
//             if(!empty($params['transport'])) {
//                 //运输表
//                 foreach ($params['transport'] as $key => $value){
//                      unset($value['key']);
//                      $value['job_number'] =  $params['job_number'];
//                      $value['created_at'] = time();
//                      $value['updated_at'] = time();
//                      DB::name('db_transport')->insert($value);
//                 }
//             }
//
//             unset($params['declaration']);
//             unset($params['transport']);
//             $this->bondedModel->storeBy($params);
//             Db::commit();
//             return CatchResponse::success('', '新建成功');
//        } catch (\Exception $exception) {
//             Db::rollback();
//             return CatchResponse::fail('新建失败');
//        }
    }


    /**
     *
     * @time 2022/02/15 11:41
     * @param Request $request
     * @return Response
     */
    public function update(CatchAuth $auth,Request $request)
    {
        $params = $request->filterEmptyField()->post();
        Db::startTrans();
        try {
            // 基本信息
            if(!empty($params['basic'])) {
                $this->tradeModel->updateBy($params['basic']['id'],$params['basic']);
            } else {
                return CatchResponse::fail('编辑失败');
            }

            //其他信息
            if(!empty($params['otherinfo'])) {
                $data['datas'] = json_encode($params['otherinfo']);
                $data['updated_at'] = time();
                $id =  $params['otherinfo']['id'];
                unset($params['otherinfo']['id']);
                Db::table('db_otherinfo')->where('id', $id)->update($data);
            }

            //报关单
            if(!empty($params['declaration'])) {
                $params['declaration']['updated_at'] = time();
                if(!empty($params['declaration']['id'])){
                    DB::name('db_declare_document')->where('id', $params['declaration']['id'])->update($params['declaration']);
                }else{
                    $params['declaration']['job_number'] = $params['basic']['job_number'];
                    DB::name('db_declare_document')->insert($params['declaration']);
                }

            }

            //运输明细
            if(!empty($params['transport'])) {
                foreach ($params['transport'] as $key => $value) {
                    unset($value['key']);
                    $value['updated_at'] = time();
                    if(!empty($value['id'])) {
                        DB::name('db_transport')->where('id', $value['id'])->update($value);
                    } else {
                        $user = $auth->user();
                        $value['tenant_id'] = $user['tenant_id'];
                        $value['created_at'] = time();
                        $value['updated_at'] = time();
                        $value['job_number'] = $params['basic']['job_number'];
                        DB::name('db_transport')->insert($value);
                    }
                }
            }
            Db::commit();
            return CatchResponse::success('', '编辑成功');
        } catch (\Exception $exception) {
            echo $exception;
            Db::rollback();
            return CatchResponse::fail('编辑失败');
        }
//         $params = $request->filterEmptyField()->post();
//         Db::startTrans();
//         try {
//             $params['id'] = $params['id'];
//             if(!empty($params['declaration'])) {
//                 //报关表
//                 foreach ($params['declaration'] as $key => $value) {
//                     unset($value['key']);
//                     $value['updated_at'] = time();
//                     if(!empty($value['id'])) {
//                         DB::name('db_declare_document')->where('id', $value['id'])->update($value);
//                     } else{
//                         $value['created_at'] = time();
//                         $value['job_number'] = $params['job_number'];
//                         DB::name('db_declare_document')->insert($value);
//                     }
//                 }
//             }
//             if(!empty($params['transport'])) {
//                 //运输表
//                 foreach ($params['transport'] as $key => $value) {
//                     unset($value['key']);
//                     $value['updated_at'] = time();
//                     if(!empty($value['id'])) {
//                         DB::name('db_transport')->where('id', $value['id'])->update($value);
//                     } else {
//                        $value['created_at'] = time();
//                        $value['job_number'] = $params['job_number'];
//                        DB::name('db_transport')->insert($value);
//                     }
//                 }
//             }
//             unset($params['declaration']);
//             unset($params['transport']);
//             $this->bondedModel->updateBy($params['id'],$params);
//             Db::commit();
//             return CatchResponse::success('', '编辑成功');
//        } catch (\Exception $exception) {
//             Db::rollback();
//             return CatchResponse::fail('编辑失败');
//        }
    }

    /**
     *
     * @throws \think\db\exception\DbException
     * @return \think\response\Json
     */
    public function delete(Request $request)
    {
        $id = $request->post('id');
         Db::startTrans();
         try {
             $job_number = $this->bondedModel->where('id',$id)->value('job_number');
             $data['deleted_at'] = time();
             DB::name('db_declare_document')->where('job_number','in',$job_number)->update($data);
             DB::name('db_transport')->where('job_number','in',$job_number)->update($data);
             $this->bondedModel->deleteBy($id);
             Db::commit();
             return CatchResponse::success('', '删除成功');
        } catch (\Exception $exception) {
             Db::rollback();
             return CatchResponse::fail('删除失败');
        }
    }
}